import { isObjectLike, isArray, cloneDeep, merge } from 'lodash'
import { initPublicConfig } from '../assets/public'

function commonRender(props) {
  const { config, target } = props
  let result = cloneDeep(target)
  if (isObjectLike(target)) {
    if (isArray(target)) {
      result = result.map((item) => merge({}, config, item))
    } else {
      result = merge({}, config, result)
    }
  } else {
    result = config
  }
  return result
}

function singleSeriesRender({ data, chartTheme }) {
  const t = cloneDeep(data)
  // 覆盖rich中的字体颜色
  if (t.label && t.label.rich) {
    Object.keys(t.label.rich).forEach((item) => {
      t.label.rich[item] = merge(t.label.rich[item], {
        color: t.label.rich[item].color || chartTheme.labelColor
      })
    })
  }
  return t
}

function seriesRender(props) {
  const { chartTheme, target } = props
  let result = []
  const commonRadius = [40, 60]
  const basics = {
    label: {
      normal: {
        show: true,
        color: chartTheme.labelColor,
        fontSize: 12,
        position: 'outside'
      }
    },
    labelLine: {
      show: true
    },
    type: 'pie',
    radius: [`${commonRadius[0] - 1}%`, `${commonRadius[1] - 1}%`]
  }
  if (isObjectLike(target)) {
    if (isArray(target)) {
      target.forEach((item, index) => {
        let resultItem = merge(basics, {
          radius: [`${commonRadius[0] - 24 * index}%`, `${commonRadius[1] - 24 * index}%`],
          ...item
        })
        resultItem = singleSeriesRender({ data: resultItem, chartTheme })
        result.push(resultItem)
      })
    }
  } else {
    result = merge(basics, target)
    result = singleSeriesRender({ data: result, chartTheme })
  }
  return result
}

function setRules({ theme }) {
  const publicConfig = initPublicConfig(theme)
  return {
    grid: {
      normal: {
        containLabel: true
      },
      render: commonRender
    },
    tooltip: {
      normal: {
        ...publicConfig.tooltip,
        trigger: 'item'
      },
      render: commonRender
    },
    legend: {
      normal: {
        ...publicConfig.legend,
        bottom: 12
      },
      render: commonRender
    },
    title: {
      normal: {
        show: true,
        textStyle: {
          color: theme.normalFontColor,
          fontSize: 14,
          fontWeight: 400
        }
      },
      render: commonRender
    },
    series: {
      render: seriesRender
    }
  }
}

function pullNewOptions({ option = {}, theme, chartTheme }) {
  const originOption = cloneDeep(option)
  const normalConfig = {
    color: chartTheme.seriesColor,
    grid: true,
    tooltip: true,
    legend: true
  }
  const newOption = merge({}, normalConfig, originOption)
  const result = {}
  const rules = setRules({ data: newOption, theme, chartTheme })
  const keys = Object.keys(newOption)
  keys.forEach((key) => {
    const target = rules[key]
    if (newOption[key]) {
      result[key] = target
        /*eslint-disable*/
        ? target.render({
          config: target.normal, // 默认参数
          target: newOption[key], // 传入参数
          chartTheme,
          theme,
          key, // 参数配置
          originOption: newOption // 全部参数
        })
        : newOption[key]
    }
  })
  return result
}

export default pullNewOptions
